﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;

namespace Shoozla.Helper
{
    public class TxtTraceListener : TraceListener
    {
        public String Log { get; set; }


        public TxtTraceListener(string filename)
            :  base(filename)
        {
            Log = DateTime.Now.ToLocalTime() + " | Shoozla started " + Environment.NewLine;

            if (!Trace.Listeners.Contains(this))
            {
                Trace.Listeners.Add(this);
            }
        }

        public override void Write(string o)
        {
            Log += DateTime.Now.ToLocalTime() + " | " + o.Trim() + Environment.NewLine;
        }

        public override void WriteLine(string o)
        {
            Log += DateTime.Now.ToLocalTime() + " | " + o.Trim() + Environment.NewLine;
        }

        public void Save()
        {
            using (StreamWriter file = new StreamWriter(Name,false))
            {
                file.Write(Log);
                file.Flush();
            }
        }
    }
}